// utf8.hpp
// Basic functions for handling UTF-8 encoded text. Note that you can
// also easily read out Unicode code-points, which are 32-bit unsigned
// integers.
//
// Copyright (C) 2008  Maksim Sipos <msipos@mailc.net>
//
// This file is a part of the Ripe language project.
//
// Ripe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#ifndef R_UTF8_HPP
#define R_UTF8_HPP

#include "standard.hpp"
#include "util/exception.hpp"

// This is a 32-bit Unicode code-point.
typedef uint32_t UniChar;

namespace Ripe {
  // Exceptions thrown by UTF-8 code.
  class ErrorUTF8 : public Exception {};

  // An operation was requested in a middle of a UTF-8 codepoint. Seek
  // forward or backward and attempt reading again.
  class ErrorUTF8Seek : public ErrorUTF8 {};

  // The given UTF-8 data is invalid (not encoded properly, or the UniChar
  // given is outside the allowed Unicode range.
  class ErrorUTF8Invalid : public ErrorUTF8 {};

  // To read or write the given unicode point, the buffer must be expanded.
  class ErrorUTF8Limited : public ErrorUTF8 {};

  // Reads one UniChar from the buffer given in str, taking care
  // not to read byte at limit or beyond it.
  UniChar read_uni_char(const char** str, const char* limit);

  // Write one UniChar to the buffer given in str, taking care not to write the
  // byte at limit or beyond it.
  void write_uni_char(char** str, const char* limit, UniChar c);
}

#endif // R_UTF8_HPP
